<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>基础代码分类</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<style>
		.layui-input{width:130px;}
		.layui-form-label{width:auto;}
		.layui-form-item .layui-input-inline{width: 130px;}
		</style>
	</head>
	<body class="pear-container">

		<div class="layui-card">
			<div class="layui-card-body">
				<table id="type-table" lay-filter="type-table"></table>
			</div>
		</div>

		<script type="text/html" id="type-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		</script>

		<script type="text/html" id="type-bar">
			<!--
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
			-->
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<script type="text/html" id="type-enable">
			<input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" checked = "{{ d.enable == 0 ? 'true' : 'false' }}">
		</script>

		<script type="text/html" id="type-createTime">
			{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd')}}
		</script>

		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let MODULE_PATH = "/{$MODULE_NAME}/Basecode/";

				let cols = [
					[
						{
							title: '编号',
							field: 'type_no',
							align: 'center',
							width: 100
						},
						{
							title: '分类名称',
							field: 'type_name',
							align: 'center',
							width: 150
						},
						{
							title: '操作',
							toolbar: '#type-bar',
							align: 'center',
						}
					]
				]

				table.render({
					elem: '#type-table',
					url: MODULE_PATH+"type?data=json",
					page: false,
					cols: cols,
					skin: 'line',
					toolbar: '#type-toolbar',
					defaultToolbar: []
				});

				table.on('tool(type-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(type-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(type-query)', function(data) {
					table.reload('type-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});

				form.on('switch(type-enable)', function(obj) {
					layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
				});

				window.add = function(obj) {
					layer.open({
						type: 2,
						title: '新增基础代码分类',
						shade: 0.1,
						area: ['400px', '250px'],
						content: MODULE_PATH + 'editType.html'
					});
				}
				
				window.edit = function(obj) {
					var _type_no=obj.data.type_no;
					layer.open({
						type: 2,
						title: '修改基础代码分类',
						shade: 0.1,
						area: ['400px', '250px'],
						content: MODULE_PATH + 'editType/type_no/'+_type_no+'.html'
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该分类?', {
						icon: 3,
						title: '删除提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "typeDel/type_no/" + obj.data['type_no'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('type-table');
				}
			})
		</script>
	</body>
</html>
